package day1;

import java.util.Arrays;

/**
 * @author pacai
 * @version 1.0
 */
public class BSNearLeft {


    public static void main(String[] args) {
        int times = 100000;
        int maxLen = 1000;
        int maxVal = 80000;
        for (int i = 0; i < times; i++) {
            int val = (int)(Math.random() * maxVal);
            int[] arr = getRanArr(maxLen,maxVal);
            Arrays.sort(arr);
            //if(findNoMoreIndex(arr,val) == findNoMoreIndex(arr,val)) continue;
            if(mostLeftNoLessNumIndex(arr,val) == findNoLessIndex(arr,val)) continue;
            System.out.println("Fucking");
            System.exit(0);
        }
        System.out.println("is ok");
    }


    public static int[] getRanArr(int maxLen,int maxVal){
        int[] arr = new int[(int)(Math.random() * maxLen) + 1];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random() * maxVal + 1) - (int)(Math.random() * maxVal);
        }
        return arr;
    }


    public static int findNoMoreIndex(int[] arr,int num){
        if(arr == null || arr.length == 0) return -1;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] <= num) return i;
        }
        return -1;
    }

    public static int findNoLessIndex(int[] arr, int num){
        if(arr == null || arr.length == 0) return -1;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] >= num) return i;
        }
        return -1;
    }

    public static int mostLeftNoLessNumIndex(int[] arr,int num) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int ans = -1;
        int l = 0;
        int r = arr.length - 1;
        while (l <= r) {
            int mid = (l + r) >>> 1;
            if(arr[mid] >= num){
                ans = mid;
                r = mid - 1;
            }else{
                l = mid + 1;
            }
        }
        return ans;
    }


    public static int mostLeftNoMoreNumIndex(int[] arr, int num){
        if(arr == null || arr.length == 0){
            return -1;
        }
        int ans = -1;
        int l = 0;
        int r = arr.length - 1;
        while(l <= r){
            int mid = (l + r) >>> 1;
            if(arr[mid] <= num){
                ans = mid;
                l = mid + 1;
            }else{
                r = mid - 1;
            }
        }
        return ans;
    }

}
